package edu.zut.cs.software.example.student.dao;

import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import edu.zut.cs.software.example.dao.GenericDao;
import edu.zut.cs.software.example.student.domain.Student;

/**
 * 
 * @author chen ming yang
 * 
 *
 */
@Repository
//@Mapper
public interface StudentDao extends GenericDao<Student, Long> {
	/**
	 * Used to login to verify user identity
	 * 
	 * @param userId
	 * @param password
	 * @return
	 */
	@Query(value = "  SELECT * FROM `example`.`t_student` WHERE `userId`=?1 AND `password`=?2 AND `state`=1 ", nativeQuery = true)
	public Student getByUserIdPassword(@Param("userId") String userId, @Param("password") String password);

	/**
	 * Used to get basic information about the user
	 * 
	 * @param ID
	 * @return
	 */
	@Query(value = "  SELECT * FROM `example`.`t_student` WHERE `ID`=?1 ", nativeQuery = true)
	public Student getInfo(@Param("ID") long ID);

	/**
	 * Used to get a list of students in the same class 
	 * 
	 * @param clas
	 * @param PageStart
	 * @param pageSize
	 * @return 
	 */
	@Query(value = "  SELECT * FROM `example`.`t_student` WHERE `clas` LIKE CONCAT('%',?1,'%')  LIMIT ?2,?3 ", nativeQuery = true)
	public List<Student> getClasStudent(@Param("clas") String clas, @Param("pageStart") int PageStart,
			@Param("pageSize") int pageSize);
	@Query(value = "  SELECT count(*) FROM `example`.`t_student` WHERE `clas` LIKE CONCAT('%',?1,'%') ", nativeQuery = true)
	public int getStudentCounts(@Param("clas") String clas);
	

	/**
	 * 
	 * @param userId：Pass in the XXX parameter and look it up according to the
	 *                    userId attribute
	 * @return Student
	 */
	public Student findByUserId(String userId);

	/**
	 * 
	 * @param userId
	 * @return boolean
	 */
	public boolean existsByUserId(String userId);

	/**
	 * Get Student by passing an int argument
	 * @param id
	 * @return
	 */
	@Query(value = " SELECT * FROM `example`.`t_student` WHERE `ID`=?1 ", nativeQuery = true)
	public Student getUpdateUser(@Param("ID") int id);
	
	
	@Query(value = " UPDATE `example`.`t_student` SET major=:major , grade=:grade WHERE ID=:id ", nativeQuery = true)
	@Modifying//
	@Transactional//事务注解
	public int editStudent(@Param("major")String major,@Param("grade")String grade,@Param("id")Long id);
}
